﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.InputOut;
using CE.CarInsurance;
using CE.Mail;

public partial class carinsurance_RenewalQuotes : System.Web.UI.Page
{
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "201")
        {
            Response.Redirect("~/carinsurance/PendingApplication.aspx");
        }

        if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "0")
        {
            Response.Redirect("~/SiteUserHome.aspx");
        }

        if (!IsPostBack)
        {
            lblError.Text = "";
            lblContent.Text = "";
            //ddlDay1.Visible = true;
            //lblFromDate.Text = Resources.General.Date + " : ";
            //lblToDate.Visible = false;
            //ddlDay2.Visible = false;
            //ddlMonth2.Visible = false;
            ddlStatus.Items.Insert(0, new ListItem("", ""));

            DateTime lastday = dateTimeNow.AddDays(2);

            DateTime beforeExpDate = dateTimeNow.AddMonths(-1);

            for (int _m = 0; _m <= 4; _m++)
            {
                ddlMonth1.Items.Insert(_m, new ListItem(beforeExpDate.AddMonths(_m).ToString("MMM yyyy"), beforeExpDate.AddMonths(_m).ToString("yyyyMM")));
                ddlMonth2.Items.Insert(_m, new ListItem(beforeExpDate.AddMonths(_m).ToString("MMM yyyy"), beforeExpDate.AddMonths(_m).ToString("yyyyMM")));
            }

            ddlMonth1.Text = dateTimeNow.ToString("yyyyMM");
            ddlMonth1_SelectedIndexChanged(null, null);
            ddlDay1.Text = dateTimeNow.ToString("dd");

            ddlMonth2.Text = lastday.ToString("yyyyMM");
            ddlMonth2_SelectedIndexChanged(null, null);
            ddlDay2.Text = lastday.ToString("dd");
            
            if (Request.Params["search"] != null)
            {
                txtSearch.Text = Request.Params["search"].ToString();
            }

            ddlReportType.SelectedIndex = ddlReportType.Items.IndexOf(ddlReportType.Items.FindByValue("2"));
            lblFromDate.Text = " " + Resources.General.FromDate + " : ";
            lblToDate.Text = " " + Resources.General.ToDate + " : ";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlDay1.Visible = true;

            btnSubmit_Click(sender, e);

 
        }
        else
        {
            lblError.Text = "";
        }
    }

    #region GetAgentCallStatus

    //private void GetAgentCallStatus()
    //{
    //    string agentId = Session[General.UserId].ToString();
    //    int callType = 1;
    //    string callStatus = "4"; //ยังติดต่อหรือปิดงานไม่ได้ 
    //    string langOption = lblLangOption.Text;
    //    int orderBy = 0;

    //    string _spName = "sprocCXCRMAgentCallScheduleStatusDetails";
    //    MyDatabase _db = new MyDatabase();
    //    DataTable _dtStatusDetails = new DataTable("tblStatusDetails");
    //    SortedList _paraList = new SortedList();
    //    _paraList.Add("@AgentID", agentId);
    //    _paraList.Add("@CallType", callType);
    //    _paraList.Add("@CallStatus", callStatus);
    //    _paraList.Add("@LangOption", langOption);
    //    _paraList.Add("@OrderBy", orderBy);
    //    _dtStatusDetails = _db.GetDataTableBySP(_spName, _paraList);

    //    GridView1.DataSource = _dtStatusDetails;
    //    GridView1.DataBind();
    //}

    //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        HyperLink hplCallNow = (HyperLink)e.Row.FindControl("hplCallNow");

    //        Label lblAgentLeadID = (Label)e.Row.FindControl("lblAgentLeadID");
    //        Label lblCustomerID = (Label)e.Row.FindControl("lblCustomerID");
    //        Label lblCarID = (Label)e.Row.FindControl("lblCarID");
    //        Label lblCallTypeID = (Label)e.Row.FindControl("lblCallTypeID");

    //        Literal lblCallTypeDesc = (Literal)e.Row.FindControl("lblCallTypeDesc");
    //        Literal lblCallStatusDesc = (Literal)e.Row.FindControl("lblCallStatusDesc");
    //        string leadID = DataBinder.Eval(e.Row.DataItem, "AgentLeadID").ToString();
    //        lblAgentLeadID.Text = leadID.ToString();
    //        string callType = DataBinder.Eval(e.Row.DataItem, "CallType").ToString();
    //        lblCallTypeID.Text = callType;
    //        string carID = DataBinder.Eval(e.Row.DataItem, "CarID").ToString();
    //        lblCarID.Text = carID;
    //        string customerID = DataBinder.Eval(e.Row.DataItem, "CustomerID").ToString();
    //        lblCustomerID.Text = customerID;
    //        string callStatus = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
    //        hplCallNow.NavigateUrl = "offline/ViewCINoMatchFound.aspx?leadid=" + leadID + "&calltype=" + callType + "&carid=" + carID + "&custid=" + customerID;

    //        switch (callType)
    //        {
    //            case "100":
    //                lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.FirstFollowUpCall + "</span>";
    //                break;
    //            case "101":
    //                lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallMeRequest + "</span>";
    //                break;
    //            case "102":
    //                lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallMeBack + "</span>";
    //                break;
    //            case "103":
    //                lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallDirect + "</span>";
    //                break;
    //            default: lblCallTypeDesc.Text = "N/A"; break;
    //        }

    //        switch (callStatus)
    //        {
    //            case "1":
    //                lblCallStatusDesc.Text = "<span style='color:red;'>" + Resources.General.Pending + "</span>";
    //                break;
    //            case "2":
    //                lblCallStatusDesc.Text = Resources.General.Callback + " <span style='color:red;'>[" + Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "CallBackDateTime").ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "]</span>";
    //                break;
    //            case "3":
    //                lblCallStatusDesc.Text = "<span style='color:green;'>" + Resources.General.FollowUp + "</span>";
    //                break;
    //            default:
    //                lblCallStatusDesc.Text = "N/A";
    //                break;
    //        }

    //    }
    //}

    #endregion

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        txtReportType.Text = ddlReportType.Text;
        txtAgentId.Text = Session[CE.Global.General.UserId].ToString();
        grvApplication.SelectedIndex = -1;
        grvApplication_SelectedIndexChanged(sender, e);
        divSearch.Visible = true;
        LoadGrid();
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblError.Text = "";
        DateTime beforeExpDate = dateTimeNow;
       
        ddlMonth1.Text = beforeExpDate.ToString("yyyyMM");
        ddlMonth1_SelectedIndexChanged(null, null);
        ddlDay1.Text = beforeExpDate.ToString("dd");
       
        ddlMonth2.Text = beforeExpDate.ToString("yyyyMM");
        ddlMonth2_SelectedIndexChanged(null, null);
        ddlDay2.Text = beforeExpDate.ToString("dd");
        
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = Resources.General.Date + " : ";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = Resources.General.Date + " : ";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = " " + Resources.General.FromDate + " : ";
            lblToDate.Text = " " + Resources.General.ToDate + " : ";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlDay1.Visible = true;
        }
    }

    protected void grvApplication_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvApplication.SelectedIndex >= 0)
        {
            GridViewRow _gvr = grvApplication.SelectedRow;

            try
            {
                string LastYearRefNo = _gvr.Cells[0].Text;

                Int64 _agentLeadId = 0;
                Int64 _customerId = 0;

                if (!Int64.TryParse(_gvr.Cells[5].Text, out _customerId))
                {
                    _customerId = 0;
                }

                Int64 _carId = 0;
                Int64 _outCarID = 0;

                if (!Int64.TryParse(_gvr.Cells[6].Text, out _carId))
                {
                    _carId = 0;
                }

                Int64 _agentID = 0;

                if (!Int64.TryParse(txtAgentId.Text, out _agentID))
                {
                    _agentID = 0;
                }

                LoadRenewalAgentLeads(_customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), 100, out _agentLeadId, out _customerId, out _outCarID);

                if (_agentLeadId > 0 && _outCarID > 0)
                {
                    //string _strParameters = "leadid=" + _agentLeadId.ToString() + "&calltype=100";
                    //_strParameters += "&renew=2";
                    //_strParameters += "&custid=" + _customerId.ToString() + "&carid=" + _outCarID.ToString();
                    //_strParameters += "&refno=" + LastYearRefNo;
                    
                    if (_customerId > 0 && _carId > 0)
                    {
                        //Response.Redirect("~/carinsurance/offline/ViewCINoMatchFound.aspx?" + _strParameters.ToString());
                        
                        //string pageurl = "offline/ViewCINoMatchFound.aspx?" + _strParameters.ToString();
                        //ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('" + pageurl + "');", true);

                        string pageurl = "renewal/renew-check.aspx?custId=" + _customerId.ToString() +
                        "&carId=" + _carId.ToString() + "&refno=" + LastYearRefNo; //+ "&agentId=" + _agentID.ToString()
                        ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWinSch", "window.open('" + pageurl + "');", true);
                    }
                    else
                    {
                        lblError.Text = "Please try again later.";
                    }
                }
            }
            catch (Exception Ex)
            {
                lblError.Text = Ex.ToString();
            }
        }
    }

    protected void grvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal lblCallStatusDesc = (Literal)e.Row.FindControl("lblCallStatusDesc");
            string callType = DataBinder.Eval(e.Row.DataItem, "CallFinishType").ToString();
            string saleComplete = e.Row.Cells[7].Text;
            if (saleComplete.Trim() != "&nbsp;" || callType == "100")
            {
                LinkButton linkbutton = (LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0]; //10
                linkbutton.Enabled = false;
                linkbutton.Text = Resources.General.Closed;
            }

            switch (callType)
            {
                case "1":
                    lblCallStatusDesc.Text = "<span style='color:red;'>" + Resources.General.CallFinish + "</span>";
                    lblCallStatusDesc.Text += "<hr style='border: 1px solid #E6E6E6; bor' />";
                    break;
                case "2":
                    lblCallStatusDesc.Text = Resources.General.Callback + " <span style='color:red;'>[" + Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "CallBackDateTime").ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "]</span>";
                    lblCallStatusDesc.Text += "<hr style='border: 1px solid #E6E6E6; bor' />";
                    break;
                case "3":
                    lblCallStatusDesc.Text = "<span style='color:green;'>" + Resources.General.FollowUp + "</span>";
                    lblCallStatusDesc.Text += "<hr style='border: 1px solid #E6E6E6; bor' />";
                    break;
                default:
                    lblCallStatusDesc.Text = "";
                    break;
            }

            string sharedlink = "http://zebra/cx/";
            string cName = e.Row.Cells[1].Text;
            string cPlate = e.Row.Cells[2].Text;
            string cDate = e.Row.Cells[3].Text;
            if (cDate.Trim() != "")
            {
                HyperLink linkPdf = (HyperLink)e.Row.FindControl("linkPdf");
                string[] path = cDate.Split('/');
                linkPdf.Text = "Pdf";
                if (cPlate.Trim() != "ป้ายแดง")
                {
                    linkPdf.NavigateUrl = sharedlink + (int.Parse(path[2]) + 1) + "/" + path[1] + "/" + cPlate.Trim() + ".pdf";
                }
                else
                {
                    cName = cName.Trim().Replace(" ", "%20");
                    linkPdf.NavigateUrl = sharedlink + (int.Parse(path[2]) + 1) + "/" + path[1] + "/" + cName.Trim() + ".pdf";
                }
            }
            
        }
    }

    private void LoadRenewalAgentLeads(Int64 CustomerID, Int64 CarId, Int64 AgentId, Int32 CallType, out Int64 outAgentLeadID, out Int64 outCustomerID, out Int64 outCarID)
    {
        string _spName = "sprocCXCRMAgentRenewalLeadDetailsSelectSingleItem";
        MyDatabase _db = new MyDatabase();
        DataTable _dtCallMeAgentLeads = new DataTable("tblRenealAgentLeads");
        SortedList _paraList = new SortedList();
        _paraList.Add("@CustomerID", CustomerID);
        _paraList.Add("@CarID", CarId);
        _paraList.Add("@CallType", CallType);
        _paraList.Add("@AgentId", AgentId);
        _dtCallMeAgentLeads = _db.GetDataTableBySP(_spName, _paraList);

        outAgentLeadID = 0;
        outCustomerID = 0;
        outCarID = 0;
        if (_dtCallMeAgentLeads != null && _dtCallMeAgentLeads.Rows.Count > 0)
        {
            outAgentLeadID = Convert.ToInt64(_dtCallMeAgentLeads.Rows[0]["AgentLeadId"].ToString());
            outCustomerID = Convert.ToInt64(_dtCallMeAgentLeads.Rows[0]["CustomerId"].ToString());
            outCarID = Convert.ToInt64(_dtCallMeAgentLeads.Rows[0]["CarId"].ToString());
        }
    }

    public bool ValidDate(string YearMonth, Int32 Day)
    {
        int Year = int.Parse(YearMonth.Substring(0, 4));
        int Month = int.Parse(YearMonth.Substring(4, 2));
        bool _isValid = true;
        int daysInMonth = 0;
        string leap, monthValid, dayValid, yearValid;
        if (Year >= 0000)
        {
            yearValid = "yes";
        }
        else
        {
            yearValid = "no";
        }
        if (Year % 400 == 0)
        {
            leap = "yes";
        }
        else if (Year % 100 == 0)
        {
            leap = "no";
        }
        else if (Year % 4 == 0)
        {
            leap = "yes";
        }
        else
        {
            leap = "no";
        }

        switch (Month)
        {
            case 1: daysInMonth = 31;
                break;
            case 3: daysInMonth = 31;
                break;
            case 4: daysInMonth = 30;
                break;
            case 5: daysInMonth = 31;
                break;
            case 6: daysInMonth = 30;
                break;
            case 7: daysInMonth = 31;
                break;
            case 8: daysInMonth = 31;
                break;
            case 9: daysInMonth = 30;
                break;
            case 10: daysInMonth = 31;
                break;
            case 11: daysInMonth = 30;
                break;
            case 12: daysInMonth = 31;
                break;
        }

        if (Month >= 1 && Month <= 12)
        {
            monthValid = "yes";
        }
        else
        {
            monthValid = "no";
        }
        if (Month == 2)
        {
            if (leap == "no")
            {
                daysInMonth = 28;
            }
            else if (leap == "yes")
            {
                daysInMonth = 29;
            }
        }

        if (1 <= Day && Day <= daysInMonth)
        {
            dayValid = "yes";
        }

        else
        {
            dayValid = "no";
        }

        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes"))
        {
            _isValid = true;
        }
        else
        {
            _isValid = false;
        }

        return _isValid;
    }

    public bool Validatioin()
    {
        lblError.Text = "";
        bool _isValid = true;
        DateTime _fromDate, _toDate;
        if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            if (ddlDay1.SelectedIndex >= 0)
            {
                
                if (!ValidDate(ddlMonth1.Text, Convert.ToInt32(ddlDay1.Text)))
                {
                    _isValid = false;
                    lblError.Text = Resources.General.ErrSelectValidDate;
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = Resources.General.ErrSelectDate;
            }
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            if (ddlDay1.SelectedIndex >= 0 && ddlDay2.SelectedIndex >= 0)
            {
                if (ValidDate(ddlMonth1.Text, Convert.ToInt32(ddlDay1.Text)))
                {
                    string sltY = ddlMonth1.Text.Substring(0, 4);
                    string sltM = ddlMonth1.Text.Substring(4, 2);
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(sltY) + "/" + Convert.ToInt32(sltM) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (ValidDate(ddlMonth2.Text, Convert.ToInt32(ddlDay2.Text)))
                    {
                        string sltY2 = ddlMonth2.Text.Substring(0, 4);
                        string sltM2 = ddlMonth2.Text.Substring(4, 2);
                        _toDate = Convert.ToDateTime(Convert.ToInt32(sltY2) + "/" + Convert.ToInt32(sltM2) + "/" + Convert.ToInt32(ddlDay2.Text));
                        if (_fromDate <= dateTimeNow)
                        {
                            if (_fromDate > _toDate)
                            {
                                _isValid = false;
                                lblError.Text = Resources.General.ErrFromdatelessthanTodate;
                            }
                        }
                        
                    }
                    else
                    {
                        _isValid = false;
                        lblError.Text = Resources.General.ErrSelectValidTodate;
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = Resources.General.ErrSelectValidFromdate;
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = Resources.General.ErrSelectFromandTodate;
            }
        }
        return _isValid;
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = dateTimeNow.Year.ToString("0000") + dateTimeNow.Month.ToString("00") + dateTimeNow.Day.ToString("00");
            
            txtReportDate1.Text = ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                
                txtReportDate2.Text = ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }
            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate1 = txtReportDate1.Text.Trim();
            string _ReportDate2 = txtReportDate2.Text.Trim();
            string _SearchKey = Server.HtmlEncode(txtSearch.Text);
            Int16 _AppStatus = 1;
            Int16 _Language = Convert.ToInt16(lblLangOption.Text);

            MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
            string _spName = "[sprocCXCRMNonGGRenewalSelectedList_Test]";
            SortedList _sPara = new SortedList();

            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@ReportDate1", _ReportDate1);
            _sPara.Add("@ReportDate2", _ReportDate2);
            _sPara.Add("@AppStatus", _AppStatus);
            _sPara.Add("@SearchKey", _SearchKey);
            _sPara.Add("@UserID", txtAgentId.Text);
            _sPara.Add("@Mobile", txtMobileNoSearch.Text);

            _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
            if (_dtSummary != null && _dtSummary.Rows.Count > 0)
            {
                lblError.Text = "";
                Cache["CarResultRenew"] = _dtSummary;
                DataView dv = new DataView();
                dv = new DataView((DataTable)Cache["CarResultRenew"]);

                if (ddlStatus.SelectedItem.Value == "1")
                {
                    //Call Now
                    dv.RowFilter = "ISNULL(NewRefNo,'') = '' ";
                }
                else if (ddlStatus.SelectedItem.Value == "2")
                {
                    //Close
                    dv.RowFilter = "CallFinishType = '100' OR ISNULL(NewRefNo,'') <> '' ";
                }

                grvApplication.DataSource = dv;
                grvApplication.DataBind();

                
            }
            else
            {
                Cache.Remove("CarResultRenew");
                grvApplication.DataSource = null;
                grvApplication.DataBind();
            }
        }
        else
        {
            Cache.Remove("CarResultRenew");
            grvApplication.DataSource = null;
            grvApplication.DataBind();
        }
    }

    protected void ddlMonth1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlMonth1.Text.Length == 6)
        {
            string day1 = ddlDay1.SelectedValue;
            int sltY = int.Parse(ddlMonth1.Text.Substring(0, 4));
            int sltM = int.Parse(ddlMonth1.Text.Substring(4, 2));
            int days = DateTime.DaysInMonth(sltY, sltM);

            ddlDay1.Items.Clear();
            for (int i = 1; i <= days; i++)
            {
                string dltD = i.ToString().PadLeft(2,'0');
                ddlDay1.Items.Add(new ListItem(dltD, dltD));
            }

            ddlDay1.SelectedIndex = ddlDay1.Items.IndexOf(ddlDay1.Items.FindByValue(day1));
        }
    }
 
    protected void ddlMonth2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlMonth2.Text.Length == 6)
        {
            string day2 = ddlDay2.SelectedValue;
            int sltY = int.Parse(ddlMonth2.Text.Substring(0, 4));
            int sltM = int.Parse(ddlMonth2.Text.Substring(4, 2));
            int days = DateTime.DaysInMonth(sltY, sltM);

            ddlDay2.Items.Clear();
            for (int i = 1; i <= days; i++)
            {
                string dltD = i.ToString().PadLeft(2, '0');
                ddlDay2.Items.Add(new ListItem(dltD, dltD));
            }

            ddlDay2.SelectedIndex = ddlDay2.Items.IndexOf(ddlDay2.Items.FindByValue(day2));
        }
    }

    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (Cache["CarResultRenew"] != null)
            {
                DataView dv = new DataView();
                dv = new DataView((DataTable)Cache["CarResultRenew"]);

                if (ddlStatus.SelectedItem.Value == "1")
                {
                    //Call Now
                    dv.RowFilter = "ISNULL(NewRefNo,'') = '' ";
                }
                else if (ddlStatus.SelectedItem.Value == "2")
                {
                    //Close
                    dv.RowFilter = "CallFinishType = '100' OR ISNULL(NewRefNo,'') <> '' ";
                }

                grvApplication.DataSource = dv;
                grvApplication.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }
    protected void btnLead_Click(object sender, EventArgs e)
    {
        //Response.Redirect("renewal-lead-list.aspx");
        Response.Write("<script>");
        Response.Write("window.open('renewal-lead-list.aspx','_blank')");
        Response.Write("</script>");
    }
}
